<?php
if (!(defined('IN_IA'))) 
{
    exit('Access Denied');
}

//服务类设备
class Sgoods_MxShopModel {

    private $sGoodsTable = '';
    private $sGoodsTypePriceTable = '';
    private $sOrderTable = '';

    public function __construct()
    {
        $this->sGoodsTable = tablename('mx_shop_service_goods');
        $this->sGoodsTypePriceTable = tablename('mx_shop_service_goods_type_price');
        $this->sOrderTable = tablename('mx_shop_service_order');
    }


    /*
	 * 根据设备号获取设备信息
	 * @param string $number 设备号
     * @param array $select 查询的字段
	 * @return array/null
 	 * */
    public function getDetail($number='', $select=array())
    {
        global $_W;

        if(empty($number))
        {
            return null;
        }

        $info = '*';
        if(!empty($select) && is_array($select))
        {
            $info = array('id');
            foreach ($select as $item)
            {
                if(is_string($item)){
                    $info[] = $item;
                }
            }
            $info = implode(',', $info);
        }

        $sql = 'SELECT '.$info.' FROM '.$this->sGoodsTable;
        $where = ' WHERE docking_number=:number AND uniacid=:uniacid AND status=1 AND deleted=0';
        $limit = ' LIMIT 1';
        $params = array(':number'=>$number, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$where.$limit, $params);
    }

    /*
	 * 根据设备id获取设备信息
	 * @param string $id 设备id
     * @param array $select 查询的字段
	 * @return array/null
 	 * */
    public function getDetailById($id='', $select=array())
    {
        global $_W;

        if(empty($id))
        {
            return null;
        }

        $info = '*';
        if(!empty($select) && is_array($select))
        {
            $info = array('id');
            foreach ($select as $item)
            {
                if(is_string($item)){
                    $info[] = $item;
                }
            }
            $info = implode(',', $info);
        }

        $sql = 'SELECT '.$info.' FROM '.$this->sGoodsTable;
        $where = ' WHERE id=:id AND uniacid=:uniacid AND status=1 AND deleted=0';
        $limit = ' LIMIT 1';
        $params = array(':id'=>$id, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$where.$limit, $params);
    }

    /*
	 * 根据表名获取详情
	 * @param int $t 表名
	 * @param int $id 表id
	 * @param array $select 要查询的字段
	 * @return array/null
 	 * */
    public function getDetailToTableById($t, $id=0, $select=array())
    {
        global $_W;

        if(empty($id))
        {
            return null;
        }

        $info = '*';
        if(!empty($select) && is_array($select))
        {
            $info = array('id');
            foreach ($select as $item)
            {
                if(is_string($item)){
                    $info[] = $item;
                }
            }
            $info = implode(',', $info);
        }

        $sql = 'SELECT '.$info.' FROM '.$t;
        $where = ' WHERE id=:id AND uniacid=:uniacid AND status=1 AND deleted=0';
        $limit = ' LIMIT 1';
        $params = array(':id'=>$id, ':uniacid'=>$_W['uniacid']);

        return pdo_fetch($sql.$where.$limit, $params);
    }

    /*
	 * 根据设备号获取价格列表
	 * @param string $number 设备号
	 * @return array/null
 	 * */
    public function getTypePriceListByGNumber($number='')
    {
        global $_W;

        $sgoods = $this->getDetail($number, array('type_id'));

        if(empty($sgoods))
        {
            return null;
        }

        $sql = 'SELECT * FROM '.$this->sGoodsTypePriceTable;
        $where = ' WHERE type_id=:type_id AND uniacid=:uniacid AND status=1 AND deleted=0';
        $limit = ' LIMIT 100';
        $params = array(':type_id'=>$sgoods['type_id'], ':uniacid'=>$_W['uniacid']);

        return pdo_fetchall($sql.$where.$limit, $params);

    }

    /*
     * 获取当前使用的设备数据
     * @return array/null
     */
    public function getInUseSgoodsByUid()
    {
        //进行中的订单
        $orders = m('sorder')->getInUseSgoodsByNumber();
        if(empty($orders))
        {
            return null;
        }

        $res = array();
        if(!empty($goodsids))
        {
            foreach ($goodsids as $index=>$item)
            {
                $sgoods = $this->getDetailById($item['goods_id'], array('docking_number', 'name'));
                $price = $this->getDetailToTableById($item['price_id']);
                $res[$index]['title'] = $sgoods['name'];
                $res[$index]['number'] = $sgoods['docking_number'];
                $res[$index]['ordersn'] = $item['ordersn'];
                $res[$index]['usetime'] = ($price['usetime'] * 60);
                $res[$index]['stime'] = ($price['usetime'] * 60) - (time() - $item['opentime']);
            }
        }

        return $res;
    }

    /*
     * 根据设备号开启设备
     * @param string $number 设备号
     * @return bool
     */
    public function openSGoodsByNumber($number)
    {
        //获取设备正进行中的订单
        $order = m('sorder')->getInUseSgoodsByNumber($number);
        if(empty($order)){
            return -1;
        }

        $sgoods = $this->getDetailById($order['goods_id'], array('isnormal'));

        //开启设备
        $res = false;

        //工作中
        if($res)
        {
            if ($sgoods['isnormal'] != 2) {
                $data_goods = array(
                    'isnormal' => 2,
                    'updatetime' => time()
                );
                pdo_update('mx_shop_service_goods', $data_goods, array('id' => $order['goods_id'], 'uniacid' => $order['uniacid']));
            }

            //更新订单
            if (empty($order['isopen']) || $order['isopen'] == 0) {
                $orderdd = array(
                    'isopen' => 1,
                    'opentime' => time()
                );
                pdo_update('mx_shop_service_order', $orderdd, array('id' => $order['id'], 'uniacid' => $order['uniacid']));
            }
        }

        return $res;
    }

    /*
    * 根据设备号关闭设备
    * @param string $number 设备号
    * @return bool
    */
    public function closeSGoodsByNumber($number)
    {
        //获取设备正进行中的订单
        $order = m('sorder')->getInUseSgoodsByNumber($number);
        if(empty($order))
        {
            return -1;
        }

        $sgoods = $this->getDetailById($order['goods_id'], array('isnormal'));
        if($sgoods['isnormal'] != 2 || $order['isopen'] != 1)
        {
            return -2;
        }

        //关闭设备
        $res = false;


        return $res;
    }

}
